package process2

import "fmt"

var userMgr *UserMgr

type UserMgr struct {
	onlineUsers map[int]UserProcess
}

func init() {
	userMgr = &UserMgr{
		onlineUsers: make(map[int]UserProcess, 1024),
	}
}

func (that *UserMgr) AddOnineUser(up *UserProcess) {
	that.onlineUsers[up.UserId] = *up
	up.NotifyOthersOnlineUser(up)
}
func (that *UserMgr) DelOnineUser(up *UserProcess) {
	delete(that.onlineUsers, up.UserId)
}
func (that *UserMgr) GetALLonlineUser() map[int]UserProcess {
	return userMgr.onlineUsers
}
func (that *UserMgr) GetOnlineUserById(userId int) (up *UserProcess, err error) {
	userProcess, ok := that.onlineUsers[userId]
	if !ok {
		err = fmt.Errorf("用户%d当前不在线", userId)
	}
	up = &userProcess
	return
}
